﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class LichSuNguoiDungDAO
    {
        public static List<LichSuNguoiDungDTO> getAll()
        {
            List<LichSuNguoiDungDTO> result = new List<LichSuNguoiDungDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LICHSUNGUOIDUNGs.ToList();

            foreach (var ds in query)
            {
                LichSuNguoiDungDTO dto = new LichSuNguoiDungDTO();

                dto.MaLichSuNguoiDung = ds.MaLichSuNguoiDung;
                dto.MaNguoiDung = ds.MaNguoiDung;
                dto.ThoiGianDangNhap = ds.ThoiGianDangNhap.Value;
                dto.ThoiGianDangXuat = ds.ThoiGianDangXuat.Value;                

                result.Add(dto);
            }

            return result;
        }

        public static LichSuNguoiDungDTO getAlongCode(int maLSNguoiDung)
        {
            LichSuNguoiDungDTO result = new LichSuNguoiDungDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LICHSUNGUOIDUNGs.Where(dm => dm.MaLichSuNguoiDung == maLSNguoiDung).Single();

            result.MaLichSuNguoiDung = query.MaLichSuNguoiDung;
            result.MaNguoiDung = query.MaNguoiDung;
            result.ThoiGianDangNhap = query.ThoiGianDangNhap.Value;
            result.ThoiGianDangXuat = query.ThoiGianDangXuat.Value;

            return result;
        }

        public static void Insert(LichSuNguoiDungDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUNGUOIDUNG lichsu = new LICHSUNGUOIDUNG();
            lichsu.MaNguoiDung = _insert.MaNguoiDung;
            lichsu.ThoiGianDangNhap = _insert.ThoiGianDangNhap;
            lichsu.ThoiGianDangXuat = _insert.ThoiGianDangXuat;            

            dbRaoVat.LICHSUNGUOIDUNGs.InsertOnSubmit(lichsu);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(LichSuNguoiDungDTO _delete)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUNGUOIDUNG lichsu = new LICHSUNGUOIDUNG();
            lichsu.MaLichSuNguoiDung = _delete.MaLichSuNguoiDung;
            lichsu.MaNguoiDung = _delete.MaNguoiDung;
            lichsu.ThoiGianDangNhap = _delete.ThoiGianDangNhap;
            lichsu.ThoiGianDangXuat = _delete.ThoiGianDangXuat;

            dbRaoVat.LICHSUNGUOIDUNGs.DeleteOnSubmit(lichsu);
            dbRaoVat.SubmitChanges();
        }

        public static void Update(LichSuNguoiDungDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LICHSUNGUOIDUNG _update = (from n in dbRaoVat.LICHSUNGUOIDUNGs
                                 where n.MaLichSuNguoiDung == dto.MaLichSuNguoiDung
                                 select n).Single();

            _update.MaNguoiDung = dto.MaNguoiDung;
            _update.ThoiGianDangNhap = dto.ThoiGianDangNhap;
            _update.ThoiGianDangXuat = dto.ThoiGianDangXuat;            

            dbRaoVat.SubmitChanges();
        }
    }
}
